package com.xiaomo.gitee.datamanager.common.result;

import java.lang.reflect.Field;

public class Code {

    @CodeInfo("账号或密码错误")
    public static final String LOGIN_FAILED_INCORRECT_PASSWORD = "40302";

    @CodeInfo("没有权限访问")
    public static final String ACCESS_DENIED = "40301";

    @CodeInfo("验证失败")
    public static final String AUTHENTICATION_FAILED = "40101";

    @CodeInfo("登录已过期")
    public static final String LOGIN_EXPIRED = "40102";

    @CodeInfo("未知错误")
    public static final String UNKNOWN_ERROR = "500";

    @CodeInfo("服务器出现错误，请联系管理员")
    public static final String SERVER_ERROR = "512";

    @CodeInfo("处理成功")
    public static final String SUCCESS = "200";

    @CodeInfo("用户名已经存在")
    public static final String USERNAME_EXISTED = "40901";

    @CodeInfo("目标资源已存在")
    public static final String RESOURCE_EXISTED = "40902";

    @CodeInfo("用户已经登录")
    public static final String USER_HAS_LOGIN = "40903";

    @CodeInfo("用户名未找到")
    public static final String USERNAME_NOTFOUND = "40401";

    @CodeInfo("客户端错误")
    public static final String CLIENT_FAILED = "400";

    public static String getInfo(String code) {
        Field[] fields = Code.class.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            try {
                if (code.equals(fields[i].get(null))) {
                    return fields[i].getAnnotation(CodeInfo.class).value();
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
        return "出现未知错误，请联系管理员";
    }

}
